<?php

namespace app\api\model;

use think\Model;

class Admin extends Model
{
    protected $login_rule =   [
        'username'   => 'require|between:5-15',
        'password'   => 'require|between:5-15',
    ];

    protected $login_message  =   [
        'username.require' => '名称必须',
        'username.between' => '账号长度在5-15之间',
        'password.require' => '请输入登录密码',
        'password.between' => '密码长度在5-15之间'
    ];

    public function login_vali($data)
    {

        try {

            validate($this->login_rule,$this->login_message)->check($data);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }

    protected $add_rule =   [
        'username'   => 'require',
        'password'   => 'require',
        'key'       =>'require'
    ];

    protected $add_message  =   [
        'username.require' => '名称必须',
//        'username.between' => '账号长度在5-15之间',
        'password.require' => '请输入登录密码',
//        'password.between' => '密码长度在5-15之间',
        'key.require' => '加密字符错误'
    ];

    public function add_vali($data)
    {
        $info = $this->where('username',$data['username'])->find();
        if($info){
            return re_warning('已经存在该账号');
        }

        try {
            validate($this->add_rule,$this->add_message)->check($data);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }
    public function add($data)
    {
        $this->startTrans();
        try {
            $this->allowField(['name','role_id','username','password','key','status','time','content'])->save($data);
            $this->commit();
            return re_success("添加成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $this->rollback();
            return re_error($e->getMessage());
        }
    }

    protected $edit_rule =   [
        'id'   => 'require',
        'username'   => 'require',
    ];

    protected $edit_message  =   [
        'id.require'=>'缺失的用户',
        'username.require'=>'缺失的用户',
    ];

    public function edit_vali($data)
    {

        try {
            validate($this->edit_rule,$this->edit_message)->check($data);
            $info = $this->where('username',$data['username'])->find($data['id']);
            if(!$info){
                return re_error('非法操作，已被记录');
            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }

    public function edit($data)
    {
        $model = Admin::find($data['id']);
        $model->startTrans();
        try {
            $model->allowField(['name','role_id','username','password','status','content'])->save($data);
            $model->commit();
            return re_success("修改成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
    protected $dele_rule =   [
        'id'   => 'require',
    ];

    protected $dele_message  =   [
        'id.require'=>'缺失的用户',
    ];
    public function dele_vali($data)
    {
        try {
            validate($this->dele_rule,$this->dele_message)->check($data);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }
    public function dele($id)
    {
        $model = Admin::find($id);
        $model->startTrans();
        try {
            $model->delete();
            $model->commit();
            return re_success("删除成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
}